// pages/carts/carts.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        count: null,
        carts: null,
        checked: false,
        checkedAll: false,
        num: 0, // 合计
        newarrs: null,
        newarrs2: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getcarts()
    },
    // 去逛逛
    emptyfn() {
        wx.switchTab({
            url: '../index/index',
        })
    },
    // 复选框
    onCheck(event) {
        // console.log(event)
        let index = event.currentTarget.dataset.id
        let value = event.detail.value
        this.data.carts[index].checked = value.length == 1 ? true : false;
        // 获取所有被选中的元素
        let newarrs = this.data.carts.filter(item => {
            return item.checked == true
        })
        // console.log("newarrs", newarrs)
        // 单个控制全选
        if (newarrs.length == this.data.carts.length) {
            this.setData({
                checkedAll: true
            })
        } else {
            this.setData({
                checkedAll: false
            })
        }
        // 选中的商品合计
        let num = newarrs.reduce((num, item) => {
            this.setData({
                count: item.count
            })
            return num + item.price * item.count
        }, 0)
        this.setData({
            carts: this.data.carts,
            newarrs,
            num,
        })
    },
    // 全选
    onCheckall(event) {
        this.setData({
            checkedAll: event.detail,
            checked: event.detail,
        });
        if (this.data.checkedAll == true) {
            for (var i = 0; i < this.data.carts.length; i++) {
                this.data.carts[i].checked = true
            }
            // 修改总价
            // 获取所有被选中的元素
            // console.log(this.data.carts)
            let newarrs = this.data.carts.filter(item => {
                return this.data.checked == true
            })
            // 选中的商品合计
            let num = newarrs.reduce((num, item) => {
                return num + item.price * item.count
            }, 0)
            // console.log(newarrs)
            this.setData({
                carts: this.data.carts,
                newarrs,
                num,
            })
        } else if (this.data.checkedAll == false) {
            for (var i = 0; i < this.data.carts.length; i++) {
                this.data.carts[i].checked = false
            }
            // 修改总价
            // 获取所有被选中的元素
            // console.log(this.data.carts)
            let newarrs = this.data.carts.filter(item => {
                return this.data.checked == true
            })
            // 选中的商品合计
            let num = newarrs.reduce((num, item) => {
                return num + item.price * item.count
            }, 0)
            // console.log(newarrs)
            this.setData({
                carts: this.data.carts,
                newarrs,
                num,
            })
        }

    },
    // 步进器
    onChange(res) {
        console.log(res.detail)
        this.setData({
            count: res.detail
        })
    },
    // 购物车商品删除
    deletefn(event) {
        let that = this
        let id = event.currentTarget.dataset.id
        const {
            position,
            instance
        } = event.detail;
        switch (position) {
            case 'cell':
                instance.close();
                break;
            case 'right':
                instance.close();
                wx.showModal({
                    title: '提示',
                    content: '确定删除吗',
                    success(res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                            that.data.carts.splice(id, 1)
                            wx.setStorageSync('bookcarts', that.data.carts)
                            // 修改总价
                            // 获取所有被选中的元素
                            let newarrs = that.data.carts.filter(item => {
                                return item.checked == true
                            })
                            // 选中的商品合计
                            let num = newarrs.reduce((num, item) => {
                                return num + item.price * item.count
                            }, 0)
                            console.log("111", newarrs)
                            that.setData({
                                carts: that.data.carts,
                                newarrs,
                                num,
                            })
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                });
                break;
        }
    },
    // 结算
    jiesuanfn(event) {
        console.log(this.data.newarrs)
        wx.navigateTo({
            url: '/pages/order/order?orders=' + JSON.stringify(this.data.newarrs),
        })
        wx.setStorageSync("bookcarts", this.data.carts)
    },
    // 获取购物车的本地存储
    async getcarts() {
        let carts = wx.getStorageSync('bookcarts')
        this.setData({
            carts
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    onTabItemTap() {
        this.getcarts()
        this.setData({
            checked: false,
            num: 0
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})